package com.xxxx.manager.controller;

import com.alibaba.druid.util.StringUtils;
import com.xxxx.common.result.FileResult;
import com.xxxx.common.result.RespBean;
import com.xxxx.manager.pojo.Goods;
import com.xxxx.manager.pojo.GoodsCategory;
import com.xxxx.manager.pojo.GoodsImages;
import com.xxxx.manager.vo.Page;
import com.xxxx.manager.service.FileService;
import com.xxxx.manager.service.GoodsCategoryService;
import com.xxxx.manager.service.GoodsImagesService;
import com.xxxx.manager.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
public class GoodsCategoryController {

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @Resource
    private GoodsService goodsService;

    @Autowired
    private FileService fileService;

    @Autowired
    private GoodsImagesService goodsImagesService;

    /**
     * 商品分类展示商品级联列表
     * @param model
     * @return
     */
    @RequestMapping("goods/category/list")
    public String categoryList(Model model) throws Exception {
        List<GoodsCategory> categoryList= goodsCategoryService.selectGoodsCategoryAll();
        model.addAttribute("gcvList",categoryList);
        return "goods/category/category-list";
    }

    /**
     * 新增分类展示顶级分类
     * @param model
     * @return
     */
    @RequestMapping("goods/category/add")
    public String categoryAdd(Model model){
        List<GoodsCategory> gcList = goodsCategoryService.selectCategoryTopList();
        model.addAttribute("gcList",gcList);
        return "goods/category/category-add";
    }

    /**
     * 新增分类展示对应的下级分类
     * @param
     * @return
     */
    @GetMapping("goods/category/{id}")
    @ResponseBody
    public List<GoodsCategory> selectCategoryList(@PathVariable Short id){
        return goodsCategoryService.selectListByParentId(id);
    }


    /**
     * 商品分类-新增保存
     * @param goodsCategory
     * @return
     */
    @PostMapping("goods/category/save")
    @ResponseBody
    public RespBean categoryAdd(GoodsCategory goodsCategory){
        return goodsCategoryService.insertGoodsCategory(goodsCategory);
    }

    /**
     * 商品列表展示分类和品牌
     * @param model
     * @return
     */
    @GetMapping("goods/list")
    public String goodsList(Model model){
        model.addAttribute("gcList",goodsService.selectCategoryNameList());
        model.addAttribute("brandList",goodsService.selectBrandNameList());
        return "goods/goods-list";
    }

    /**
     * 商品列表分页
     * @param page
     * @return
     */
    @PostMapping("goods/listForPage")
    @ResponseBody
    public Map<String, Object> listForPage(Page page){
        return goodsService.selectPageFromGoodsAndCategory(page);
    }

    /**
     * 商品列表新增-展示分类
     * @param model
     * @return
     */
    @GetMapping("goods/add")
    public String addGoods(Model model){
        model.addAttribute("gcList",goodsCategoryService.selectCategoryTopList());
        model.addAttribute("brandList",goodsService.selectBrandNameList());
        return "goods/goods-add";
    }

    /**
     * 商品列表新增-通用信息保存
     * @param goods
     * @return
     */
    @PostMapping("goods/save")
    @ResponseBody
    public RespBean insertGoods(Goods goods){
        return goodsCategoryService.insetGoods(goods);
    }

    /**
     * 商品相册上传
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("goods/images/save")
    @ResponseBody
    public RespBean photoAlbumsToUpload(MultipartFile file,GoodsImages goodsImages) throws Exception {
        RespBean respBean = RespBean.error("error");
        FileResult fileResult = fileService.fileUpload(file);
        if (!StringUtils.isEmpty(fileResult.getFileUrl())) {
            goodsImages.setImageUrl(fileResult.getFileUrl());
            respBean = goodsImagesService.insertGoodsImages(goodsImages);
        }
        return respBean;
    }

}
